package demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import demo.model.User;
import demo.service.UserService;
import demo.utils.SysLogUtil;

@Controller
public class UserController {

	private UserService userService;
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	// Restful路径: /login?username={id}&&password={password}
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(@RequestParam("username") String userName, @RequestParam("password") String password) {
		User user = userService.login(userName, password);
		ModelAndView mav = null;
		if (user != null) {
			mav = new ModelAndView("success");
			mav.addObject(user);
		} else {
			mav = new ModelAndView("error");
		}

		SysLogUtil.print(mav.getViewName());
		return mav;
	}
}
